package com.jeff.android.lab.architecture.arouter;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

import androidx.core.app.ActivityOptionsCompat;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jeff.android.lab.BaseFragment;
import com.jeff.android.lab.R;
import com.jeff.android.lab.architecture.arouter.bean.Car;
import com.jeff.android.lab.architecture.arouter.bean.UserInfo;
import com.jeff.android.lab.architecture.arouter.server.CarRentalActivity;
import com.jeff.android.lab.commonlib.router.Logger;
import com.jeff.android.lab.commonlib.router.ThreadPoolFactory;
import com.jeff.android.lab.commonlib.router.provider.IDataProvider;
import com.jeff.android.lab.commonlib.router.Constant;
import com.jeff.utils.ToastUtil;
import com.jeff.utils.security.AESUtil;

import butterknife.OnClick;

/**
 * 阿里开源框架 ARouter 使用
 *
 * @author: duff
 * @date: 2019/3/11
 * @since: 1.0.0
 */
public class ARouterFragment extends BaseFragment {

    @Override
    protected int getRootLayout() {
        return R.layout.architercture_fragment_arouter;
    }

    /*
    * 基本用法
    * */
   @OnClick(R.id.btn_login)
    public void openLogin() {
       ARouter.getInstance()
               .build(Constant.ACTIVITY_URL_LOGIN)
               .navigation(getContext());
    }

    /*
     * Uri 跳转
     * */
    @OnClick(R.id.btn_login_with_uri)
    public void openLoginWithUri() {
        final Uri uri = Uri.parse("iyunbao://host/account/login");
        ARouter.getInstance()
                .build(uri)
                .navigation();
    }

    /*
     * 带参数 跳转
     * */
    @OnClick(R.id.btn_login_with_params)
    public void openLoginWithParams() {
        UserInfo user = new UserInfo("Jack", 29);

        ARouter.getInstance()
                .build(Constant.ACTIVITY_URL_LOGIN)
                .withString(Constant.NAME, "Jack")
                .withInt(Constant.AGE, 28)
                .withParcelable(Constant.USER, user)
                .navigation();
    }

    /*
     * 带参数(参数解析) 跳转
     * */
    @OnClick(R.id.btn_login_autowired)
    public void openLoginAutowired() {
        ARouter.getInstance()
                .build(Constant.ACTIVITY_URL_LOGIN)
                .withString(Constant.NAME, "dingfeng")
                .withInt(Constant.AGE, 28)
                .navigation();
    }

    /*
     * 带参数(自定义对象) 跳转
     * */
    @OnClick(R.id.btn_login_with_object)
    public void openWithCustomObject() {
        ARouter.getInstance()
                .build(Constant.ACTIVITY_URL_LOGIN)
                .withString(Constant.NAME, "dingfeng")
                .withInt(Constant.AGE, 28)
                .withObject("car", new Car("BMW", 100000))
                .navigation();
    }

    /*
     * 指定 flag
     * */
    @OnClick(R.id.btn_login_with_flag)
    public void openWithFlag() {
        ARouter.getInstance()
                .build(Constant.ACTIVITY_URL_LOGIN)
                .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
                .navigation();
    }


    /*
    * 获取 Fragment
    * */
    @OnClick(R.id.btn_login_fragment)
    public void openLoginFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance()
                .build(Constant.FRAGMENT_URL_LOGIN)
                .navigation();

        getChildFragmentManager()
                .beginTransaction()
                .add(R.id.container, fragment)
                .commit();

        ToastUtil.show(mContext, fragment == null ? "fragment 为 NULL" : fragment.getClass().getSimpleName());
    }

    @OnClick(R.id.btn_login_interceptor)
    public void openLoginWithInterceptor() {
        ARouter.getInstance()
                .build(Constant.ACTIVITY_URL_MINE)
                .greenChannel()
                .navigation(getContext(), new NavigationCallback() {
                    @Override
                    public void onFound(Postcard postcard) {
                        // 路由发现
                        Log.d("dingfeng", "onFound ... ");
                    }

                    @Override
                    public void onLost(Postcard postcard) {
                        // 路由丢失
                        Log.d("dingfeng", "onLost ... ");
                    }

                    @Override
                    public void onArrival(Postcard postcard) {
                        // 路由到达
                        Log.d("dingfeng", "onArrival ... ");
                    }

                    @Override
                    public void onInterrupt(Postcard postcard) {
                        // 路由被拦截
                        Log.d("dingfeng", "onInterrupt ... ");
                    }
                });
    }

    @OnClick(R.id.btn_start_result)
    public void openActivityForResult() {
        ARouter.getInstance()
                .build(Constant.ACTIVITY_URL_ONE)
                .navigation();
    }

    @OnClick(R.id.btn_transition)
    public void openActivityWithAnim() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
            ActivityOptionsCompat compat = ActivityOptionsCompat.
                    makeCustomAnimation(mContext, R.anim.slide_right_in, R.anim.slide_right_out);

            ARouter.getInstance()
                    .build(Constant.ACTIVITY_URL_MINE)
                    .withOptionsCompat(compat)
                    .navigation(getActivity());
        } else {
            ARouter.getInstance()
                    .build(Constant.ACTIVITY_URL_MINE)
                    .withTransition(R.anim.slide_right_in, R.anim.slide_right_out)
                    .navigation(getActivity());
        }
    }

    /*
    * 组件之间传递数据
    * */
    @OnClick(R.id.btn_transform_in_modules)
    public void transformDataInModules() {
        setData();

        ARouter.getInstance()
                .build(Constant.ACTIVITY_URL_HOME)
                .navigation();
    }


    public void setData() {
        IDataProvider provider = (IDataProvider) ARouter
                .getInstance()
                .build("/provider/data")
                .navigation();

        provider.setData("Data from app module.");
    }

    /*
     * 服务管理
     * */
    @OnClick(R.id.btn_service_manager)
    public void serviceManager() {
        startActivity(new Intent(getContext(), CarRentalActivity.class));
    }

    /*
     * 预处理服务
     * */
    @OnClick(R.id.btn_pretreatment_service)
    public void pretreatmentService() {
        try {
            ARouter.getInstance()
                    .build(Constant.ACTIVITY_URL_LOGIN)
                    .withString(Constant.NAME, AESUtil.encrypt("jack"))
                    .navigation();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    * 自定义线程池和Log
    * */
    @OnClick(R.id.btn_custom_thread_pool)
    public void userCustomThreadPool() {
        ARouter.setExecutor(ThreadPoolFactory.getExecutor("jack-custom-thread-pool"));
        ARouter.setLogger(new Logger());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        ToastUtil.show(getActivity(), "requestCode:" + requestCode);
    }

}
